<template>
  <div>
    <!-- <el-button type="primary" style="margin-top:20px" size="mini" @click="importInfo()">导入</el-button> -->
    <el-button type="primary" style="margin-top:20px" size="mini" @click="downLoadExcel()">导出</el-button>
    <SearchBar :searchList="searchList"   @search="search"></SearchBar>
    <el-table :data="tableData"  border style="width: 100%">
      <el-table-column prop="plantId" label="ID"  align="center"> </el-table-column>
      <el-table-column prop="plantSerialNum" label="购买序号" align="center"> </el-table-column>
      <el-table-column prop="plantNum" label="肥料编号" align="center"> </el-table-column>
      <el-table-column prop="plantName" label="肥料名称" align="center"> </el-table-column>
      <el-table-column prop="plantType" label="所属类别" align="center"> </el-table-column>
      <el-table-column prop="plantQuantity" label="购买数量" align="center"> </el-table-column>
      <el-table-column prop="ferQuantity" label="施肥数量" align="center"> </el-table-column>
      <el-table-column prop="count" label="余存数量" align="center"> </el-table-column>
      <el-table-column prop="remark" label="备注" align="center"> </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="pagination.pageNum"
      :page-sizes="[5,10, 20, 30, 40]"
      :page-size="pagination.pageSize"
      layout=" prev, pager, next,sizes"
      :total="pagination.total"
      align='center'>
    </el-pagination>
  </div>
</template>

<script>
// 组件
import CubeUpload from "@/components/CubeUpload"
//工具

export default {
  name: "manureStock",
  components: {
    CubeUpload
  },
  data() {
    return {
      searchList:[
        {placeholder:"肥料名称",type:"input", key:'plantName'},
        {placeholder:"所属类别",type:"option", key:'plantType',options:[{label:"苹果", value:"apple"},{label:"香蕉",value:"banana"}]},
        {placeholder:"余存数量",type:"input", key:'count'},
      ],
      pagination:{
        pageNum:1,
        pageSize:10,
        total:100
      },
      dialogFormVisible:false,
      tplUrl: '/car/downloadImportTemplate',
      actionUrl: '/car/importCar',
      tableData: [],
      type:"",
      searchObj:{},
    }
  },
  created(){
    // this.updatePageList()

  },
  methods:{
   //点击查询按钮
    async search(formInline){
      this.searchObj=formInline
      let res=await this.$API.spreadManureInfo.getPageList({pageNum:this.pagination.pageNum,pageSize:this.pagination.pageSize,...this.searchObj})
      if(res.code==200){
        this.tableData=res.data.rows
        this.pagination.total=res.data.rowCount
      }
    },
    //更新分页器
    async updatePageList(page=1){
      this.pagination.pageNum=page
      // this.pagination.pageSize=size
      let res=await this.$API.spreadManureInfo.getPageList({pageNum:this.pagination.pageNum,pageSize:this.pagination.pageSize,...this.searchObj})
      if(res.code==200){
        this.tableData=res.data.rows
        this.pagination.total=res.data.rowCount
      }
    },
    //修改数据
    updateData(row){
      this.type="update"
      this.$refs.add.dialogFormVisible=true
      console.log(row,555)
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
      this.gardenPlot = JSON.parse(JSON.stringify(row))
      this.plantFood = JSON.parse(JSON.stringify(row))
      this.$refs.add.form.gardenPlot = this.gardenPlot
      this.$refs.add.form.plantFood = this.plantFood
      console.log(this.$refs.add.form,'this.$refs.add.form')
      // console.log(row)
      // this.$API.spreadManureInfo.saveOrUpdate(row)
    },
    //删除数据
    async deleteData(row){
      try{
        let res=await this.$API.spreadManureInfo.deleteData({id:row.pesticidesId})
        if(res.code==200){
          this.$message({type:"success",message:"删除成功"})
          this.updatePageList()
        }
      }catch{
          this.$message({type:"error",message:"删除失败"})
      }
    },
    //监听每页展示页数的改变
    handleSizeChange(size) {
      this.pagination.pageSize=size
      this.updatePageList(this.pagination.pageNum)
      // console.log(`每页 ${size} 条`);
    },
    //监听当前页的改变
    handleCurrentChange(page) {
      this.updatePageList(page)
      // console.log(`当前页: ${page}`);
    },
    //导出按钮
    downLoadExcel(){
      // let data = {  }   //可以传入参数
      // downLoadExcel(data).then((res) => {
      //   let ex = exportData(res)
      // })
       this.$message.success('正在导出请稍候')
      const prams = this.$refs['CubeTableRender'] && this.$refs['CubeTableRender'].getParam()
      exportExcelList(prams).then(res => {
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel;charset=utf-8' })
        const href = window.URL.createObjectURL(blob) // 创建下载的链接
        const downloadElement = document.createElement('a')
        downloadElement.href = href
        downloadElement.download = decodeURI(res.headers['filename'])
        document.body.appendChild(downloadElement)
        downloadElement.click() // 点击下载
        document.body.removeChild(downloadElement) // 下载完成移除元素
        window.URL.revokeObjectURL(href) // 释放掉blob对象
      }).catch(err => {
        this.$message({
          message: '下载失败！',
          type: 'error',
          showClose: true
        })
      })
    },
    //导入按钮
    // importInfo(){
    //   // this.CubeDialogTitle = '导入车辆信息'
    //   this.dialogFormVisible = true
    //   // this.tplUrl = '/car/downloadImportTemplate'
    //   // this.actionUrl = '/car/importCar'
    //   return
    // }
  }
};
</script>

<style scoped>
</style>